<template>
  <el-dialog :title="formValidate.id ? $t('collectionManagement.management.personnelEdit') : $t('collectionManagement.management.personnelAdd')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('collectionManagement.management.model.actualName')" prop="staffName">
        <el-input v-model="formValidate.staffName" clearable :placeholder="$t('collectionManagement.management.prompt.actualName')" :maxlength="100" />
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.searchModel.phoneNumber')" prop="phonenumer">
        <el-input v-model="formValidate.phonenumer" clearable type="tel" :placeholder="$t('collectionManagement.management.prompt.phoneNumber')" :maxlength="100" />
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.searchModel.group')" prop="groupId">
        <el-col :span="16">
          <el-select v-model="formValidate.groupId" clearable :placeholder="$t('collectionManagement.management.prompt.group')" style="width: 100%" @change="groupChange">
            <el-option-group
              v-for="group in groupSelect"
              :key="group.label"
              :label="group.label"
            >
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-option-group>
          </el-select>
        </el-col>
        <el-col :span="8">
          <p style="margin-left: 5px;line-height: 15px">{{ groupType }}</p>
        </el-col>
      </el-form-item>
      <el-form-item v-if="groupType !== '催收管理岗'" :label="$t('collectionManagement.management.model.weights')" prop="weight">
        <el-select v-model="formValidate.weight" clearable :placeholder="$t('collectionManagement.management.prompt.weights')" style="width: 100%">
          <el-option
            v-for="item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.model.accountName')" prop="username">
        <el-input v-model="formValidate.username" clearable :placeholder="$t('collectionManagement.management.prompt.accountName')" :maxlength="100" />
      </el-form-item>
      <el-form-item v-if="!formValidate.id" :label="$t('collectionManagement.management.model.accountPassword')" prop="password">
        <el-input v-model="formValidate.password" clearable type="password" :placeholder="$t('collectionManagement.management.prompt.accountPassword')" :maxlength="50" />
      </el-form-item>
      <el-form-item v-else :label="$t('collectionManagement.management.model.accountPassword')">
        <el-input v-model="formValidate.password" clearable type="password" :placeholder="$t('collectionManagement.management.prompt.accountPassword')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.model.recallPhone')">
        <el-input v-model="formValidate.recallPhone" clearable :placeholder="$t('collectionManagement.management.prompt.recallPhone')" :maxlength="100" />
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.model.recallPhonePassword')">
        <el-input v-model="formValidate.recallPhonePassword" clearable type="password" :placeholder="$t('collectionManagement.management.prompt.recallPhonePassword')" :maxlength="100" />
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.searchModel.status')" prop="status">
        <el-select v-model="formValidate.status" clearable :placeholder="$t('collectionManagement.management.prompt.status')" style="width: 100%">
          <el-option
            v-for="item in $t('collectionManagement.management.status1')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('collectionManagement.management.model.freed')" prop="releaseCase">
        <el-select v-model="formValidate.releaseCase" clearable :placeholder="$t('collectionManagement.management.prompt.freed')" style="width: 100%">
          <el-option
            v-for="item in $t('collectionManagement.management.freed')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateOverdueStaff, addOverdueStaff } from '@/api/collection'
export default {
  name: 'PersonnelDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    groupSelect: {
      type: Array,
      default: function() {
        return []
      }
    },
    groupData: {
      type: Array,
      default: function() {
        return []
      }
    },
    groupType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        staffName: null,
        phonenumer: null,
        groupId: null,
        weight: null,
        username: null,
        password: null,
        recallPhone: null,
        recallPhonePassword: null,
        status: null,
        releaseCase: null
      },
      rules: {
        staffName: [{ required: true, message: this.$t('collectionManagement.management.model.actualName') + this.$t('common.null'), trigger: 'blur' }],
        phonenumer: [{ required: true, message: this.$t('collectionManagement.management.searchModel.phoneNumber') + this.$t('common.null'), trigger: 'blur' }],
        groupId: [{ required: true, message: this.$t('collectionManagement.management.searchModel.group') + this.$t('common.null'), trigger: 'blur' }],
        weight: [{ required: true, message: this.$t('collectionManagement.management.model.weights') + this.$t('common.null'), trigger: 'blur' }],
        username: [{ required: true, message: this.$t('collectionManagement.management.model.accountName') + this.$t('common.null'), trigger: 'blur' }],
        password: [{ required: true, message: this.$t('collectionManagement.management.model.accountPassword') + this.$t('common.null'), trigger: 'blur' }],
        status: [{ required: true, type: 'number', message: this.$t('collectionManagement.management.searchModel.status') + this.$t('common.null'), trigger: 'blur' }],
        releaseCase: [{ required: true, type: 'number', message: this.$t('collectionManagement.management.model.freed') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    groupChange(data) {
      this.$emit('group-dialog', data)
    },
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const formData = this.formValidate
          if (formData.id) {
            updateOverdueStaff(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addOverdueStaff(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
